package client;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class UserGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JTextArea[] areas;
	private LinkedList<String>[] buffers;
	private String[] clientNames;

	@SuppressWarnings("unchecked")
    public UserGUI(String myName, int subCount, String[] clientNames) {
	    this.clientNames=clientNames;
		setTitle(myName);
		setSize(600, 400); // default size is 0,0
		setLocation(10, 200); // default is 0,0 (top left corner)

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		buffers = (LinkedList<String>[])new LinkedList[subCount];
		Panel p = new Panel(new GridLayout());
		areas = new JTextArea[subCount];
		for (int i = 0; i < subCount; i++) {
		    buffers[i]=new LinkedList<String>();
			areas[i] = new JTextArea();
			p.add(areas[i]);
			areas[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
		}
		this.add(p);
		setVisible(true);
	}

	public void updateArea(int areaNo, String text) {
	    LinkedList<String> buffer = buffers[areaNo];
		buffer.addFirst(text);
		if(buffer.size()>50){
		    buffer.removeLast();
		}
		StringBuilder b = new StringBuilder();
		b.append(clientNames[areaNo]);
		b.append("\n");
		b.append("__________________________");
		b.append("\n");
		for(String s : buffer){
		    b.append(s);
		    b.append("\n");
		}
		areas[areaNo].setText(b.toString());

	}

}
